package com.pcxt.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Vector;

import android.util.Xml;

import org.xmlpull.v1.*;

import com.pcxt.exception.*;


public class XMLProcessor {
	
	/*
	 * private variables
	 */
	private Vector<HashMap<String,Object>> instances = null;
	private String errorMsg = "No Error Message.";
	
	
	/*
	 * public methods
	 */
	
	public HashMap<String, Object> getValues(int index) throws XException {
		 /*VALUES LIST
		 obid
		 source
		 reassigned
		 name
		 taskURLPathInfo
		 role
		 priority
		 instructions
		 description
		 eventActionMap
		 primaryBusinessObjectName
		 primaryBusinessObjectType
		 thePersistInfoCreateStamp
		 objectIcon
		 deadline
		 */
		HashMap<String, Object> result = null;
		
		if (index > instances.size())
			throw new XException ("index doesn't match qantity of instances.");
		result = instances.get(index);
		
		return result;
	}
	
	public String getValue(HashMap<String,Object> instance, String key) {
		Object result = null;
		result = instance.get(key);
		if (result != null)
			return result.toString();
		else
			return "";
	}
	
	public Vector<HashMap<String,Object>> getInstances() {
		return instances;
	}
	
	public String getExceptionMsg() {	
		return errorMsg;
	}
	
	/*
	 * constructor
	 */
	
	public static XMLProcessor initialize(InputStream ios) throws XException {
		XMLProcessor result = null;
		
		result = new XMLProcessor();
		
		result.parse(ios);
		
		
		return result;
	}
	
	
	private void parse(InputStream ios) throws XException {
		XmlPullParser parser = Xml.newPullParser();
		int eventType = 0;
		try {
			parser.setInput(ios,"UTF-8");
			
			findFirstInstance(parser);
			eventType = parser.getEventType();
			
			while (eventType != XmlPullParser.END_DOCUMENT) {
				// start to process all tags
				
				switch (eventType) {
					case XmlPullParser.START_TAG:
						String tag = parser.getName();
						if (tag.equals("INSTANCE")) {
							retrieveInstances(parser);
						}
						else if (tag.equals("exception")) {
							retrieveErrorMessage(parser);
						}
						else {
							//.. not an exact tag name
							break;
						}
						break;
					case XmlPullParser.END_DOCUMENT:
						if (true) {
							break;
						}
						break;

				}
				
				eventType = parser.next();
			}
			
		}
		
		catch (IOException ioe) {
			throw new XException (ioe.getMessage());
		}
		
		catch (XmlPullParserException xe) {
			throw new XException (xe.getMessage());
		}
		
		
	}
	
	private void retrieveErrorMessage(XmlPullParser parser) 
	throws IOException, XmlPullParserException {
		int eventType = 0;

		eventType = parser.getEventType();
		String strMessage = "";
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG) {
				String tag = parser.getName();
				if (tag.equals("MESSAGE")) {
				}
			}
			else if (eventType == XmlPullParser.END_TAG) {
				String tag = parser.getName();
				if (tag.equals("MESSAGE")) {
					errorMsg = strMessage;
					return;
				}
			}
			else if (eventType == XmlPullParser.TEXT) {
				strMessage = parser.getText();
			}
			
			eventType = parser.next();
		}
	}
	
	private void retrieveInstances(XmlPullParser parser) 
	throws IOException, XmlPullParserException {
		instances = new Vector<HashMap<String,Object>>();
		
		int eventType = 0;
		HashMap<String, Object> instance = null;

		eventType = parser.getEventType();
		String strText = "";
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG) {
				String tag = parser.getName();
				if (tag.equals("INSTANCE")) {
					// a new instance to be created.
					instance = new HashMap<String, Object>();
				}
				else if (tag.equals("Attribute")) {
					int count = parser.getAttributeCount();
					for (int i = 0; i < count; i ++) {
						String att = parser.getAttributeValue(i);
						if (att.equals("NAME")) {
							instance.put(att, strText);
						}
					}
				}
				else {
					// attribute will be added.
					//instance.put(tag, strText);
				}
			}
			else if (eventType == XmlPullParser.END_TAG) {
				String tag = parser.getName();
				if (tag.equals("INSTANCE")) {
					//  instance to be added.
					instances.add(instance);
				}
				else if (tag.equals("wt.fc.WTObject")) {
					return;
				}
				else if (tag.equals("class")) {
					if (parser.getDepth() == 2)
						return;
					else
						instance.put(tag, strText);
				}
				else {
					// attribute will be added.
					instance.put(tag, strText);
				}
			}
			else if (eventType == XmlPullParser.TEXT) {
				strText = parser.getText();
			}
			eventType = parser.next();
		}
		
	}
	
	private void findFirstInstance(XmlPullParser parser) 
	throws IOException,XmlPullParserException {
		int eventType = 0;

		eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG) {
				String tag = parser.getName();
				if (tag.equals("INSTANCE")) {
					return;
				}
				else if (tag.equals("exception")) {
					return;
				}
			}
			
			eventType = parser.next();
		}
	}
	
	
	private XMLProcessor() {
		// create from initializer only
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
